package com.runing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.runing.config.ResponseWrapper;
import com.runing.entity.RClasses;
import com.runing.entity.RCollege;
import com.runing.entity.RGrade;
import com.runing.mapper.RClassesMapper;
import com.runing.mapper.RCollegeMapper;
import com.runing.service.RClassesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.xmlbeans.impl.xb.xsdschema.All;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-03-11
 */
@Service
public class RClassesServiceImpl extends ServiceImpl<RClassesMapper, RClasses> implements RClassesService {

    @Autowired
    RClassesMapper rClassesMapper;
    @Autowired
    RCollegeMapper rCollegeMapper;
    @Autowired
    RWeekServiceImpl rWeekService;

    public ResponseWrapper homePage(){
        ArrayList<Object> Allcollege = new ArrayList<>();
        List<RCollege> allCollege = rCollegeMapper.selectList(null);
        for (RCollege rCollege : allCollege){
            HashMap<String, Object> college = new HashMap<>();
            ArrayList<Object> gradeBeanList = new ArrayList<>();
            int cid = rCollege.getCid();
            List<RGrade> allGrade = rClassesMapper.findAllGrade(cid); // 查询当前学院的所有年级
            for (RGrade rGrade : allGrade){
                HashMap<String, Object> grade = new HashMap<>();
                int gid = rGrade.getGid(); // 年级id
                List<Map> classesBeanList = rClassesMapper.findClasses(cid, gid); // 班级
                grade.put("gid",gid);
                grade.put("gname",rGrade.getGname());
                grade.put("classesBeanList",classesBeanList); // 一个年级的所有班级
                gradeBeanList.add(grade);
            }
            college.put("cid",cid);
            college.put("cname",rCollege.getCname());
            college.put("gradeBeanList",gradeBeanList);
            Allcollege.add(college);
        }
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(Allcollege);
        wrapper.getData().put("Allcollege",Allcollege);
        return wrapper;
    }

    public ResponseWrapper getWeek(){
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(homePage().getData().get("Allcollege"));
        wrapper.getData().put("semesterBeans",rWeekService.allWeeks());
        wrapper.getData().put("collegeBeanList",homePage().getData().get("Allcollege"));
        return wrapper;
    }

    public ResponseWrapper deleteClasses(Integer clid){
        if (clid == null){
            return ResponseWrapper.markCustom(false,400,"clid不能为空",null);
        }else {
            RClasses rClasses = new RClasses();
            rClasses.setClid(clid);
            rClasses.setCldelete(true);
            int result = rClassesMapper.updateById(rClasses);
            if (result == 1){
                return ResponseWrapper.markCustom(true,200,"删除成功",null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }

    public ResponseWrapper daleteGrade(Integer cid,Integer gid){
        if (cid == null || gid == null){
            return ResponseWrapper.markCustom(false,400,"cid,gid不能为空",null);
        }else {
            QueryWrapper<RClasses> classesQueryWrapper = new QueryWrapper<>();
            classesQueryWrapper.eq("cid",cid).eq("gid",gid);
            List<RClasses> classesList = rClassesMapper.selectList(classesQueryWrapper);
            RClasses rClasses = new RClasses();
            for (RClasses rClassess : classesList){
               rClasses.setClid(rClassess.getClid());
               rClasses.setCldelete(true);
               rClassesMapper.updateById(rClasses);
            }
            return ResponseWrapper.markCustom(true,200,"删除成功",null);
        }
    }

}
